'use strict';

import adminBase from './base.js';

export default class extends adminBase {
    /**
     * index action
     * @return {Promise} []
     */
    indexAction(that){
        let type = that.param('type');
        that.assign('type',type);
        return that.display();
    }
    async dataAction(that){
        let type = that.param('type');
        if(!type){
            return that.fail("类型参数错误");
        }
        let newsObj = think.service('news'),
            newsService = new newsObj();
        let list = await newsService.pageByType(type,that.param('page',1),that.param('limit',10));
        return that.success(list,"操作成功");
    }
    async delAction(that){
        let id = that.param('id');
        if(!id){
            return that.fail("id参数错误");
        }
        let newsObj = think.service('news'),
            newsService = new newsObj();
        await newsService.delById(id);
        return that.success("成功删除");
    }
    async toAction(that){
        let type = that.param('type');

        let id = that.param('id'),newsModel={};
        if(id){
            let newsObj = think.service('news'),
                newsService = new newsObj();
            newsModel = await newsService.findById(id);
        }
        that.assign('news',newsModel);
        that.assign('type',type);
        return that.display();
    }
    async saveAction(that){
        let params = that.param();
        think.log(params);
        let newsObj = think.service('news'),
            newsService = new newsObj();
        let result = await newsService.save(params);
        think.log("返回："+result);
        return that.success('ok');
    }
}